var userDataPageSize = 10;

//读取数据
function createList(pageNo = 1, pageSize = 10) {
    let matchData = {};
    let uid = $("#userIDTxt").val();
    if (!!uid) {
        matchData.uid = parseInt(uid);
    }

    var start = (pageNo - 1) * pageSize;
    apiAdminGetRecord("userNoteModel", start, pageSize, matchData, function (data) {
        if (data.code === 0) {
            setTimeout(() => {
                $('#userList').bootstrapTable('load', data.msg);
            }, 200)

        }
    });
}


//显示新增金币
function showAddGold(id, uid) {
    $("#uid").val(uid);
    $("#id").val(id)
    $('#addGoldDig').modal('show');
}


function addReply() {

    let replyMessage = $("#replyMessage").val();
    let id = $("#id").val();
    let uid = $("#uid").val();
    Swal.fire({
        title: '操作无法撤销，是否确定发送？',
        // text: "是否确定要批量添加选中文章？",
        icon: 'question',
        showCancelButton: true,
        confirmButtonColor: '#198754',
        cancelButtonColor: '#d93444',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
    }).then((result) => {
        if (result.isConfirmed) {

            apiAdminUpdateRecord("userNoteModel", { _id: id }, { replyTime: Date.now(), reply: replyMessage }, function () {
                apiSendSingleEmail(uid, '系统回复', replyMessage, function (data) {
                    if (data.code === 0) {
                        $("#replyMessage").val('');
                        $('#addGoldDig').modal('hide');
                        $.modal({ body: '操作成功' });
                        var currentPage = $('#userList').bootstrapTable('getOptions').pageNumber
                        createList(currentPage);
                    }
                });
            });



        }
    })


}



function execGrant(field, uid, count) {
    count = parseInt(count) || 0;
    if (count === 0) {
        return;
    }
    apiAdminGrant(field, uid, count, () => {
        $('#addGoldDig').modal('hide');
        $.modal({ body: '操作成功' });
        var currentPage = $('#userList').bootstrapTable('getOptions').pageNumber
        createList(currentPage);
    })
}

$(document).ready(function () {

    //数据表格初始化
    $('#userList').bootstrapTable({
        striped: true, // 是否显示行间隔色
        pageNumber: 1, // 初始化加载第一页
        pagination: true, // 是否分页
        sidePagination: 'server', // server:服务器端分页|client：前端分页
        pageSize: 10, // 单页记录数
        pageList: [10, 20, 30],
        theadClasses: "thead-light",//这里设置表头样式
        totalField: "totalCount",
        smartDisplay: false,
        buttonsClass: 'light',
        buttonsPrefix: 'btn',
        dataField: "recordArr",
        showRefresh: false,// 刷新按钮
        onPageChange: function (number, size) {
            createList(number, size);
        },
        queryParams: function (params) { // 上传服务器的参数
            var temp = {//如果是在服务器端实现分页，limit、offset这两个参数是必须的
                limit: params.limit, // 每页显示数量
                offset: params.offset, // SQL语句起始索引
            };
            return temp;
        },
        onPostBody: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        onLoadSuccess: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        columns: [{
            title: 'id',
            field: '_id',
            visible: false
        },
        {
            title: 'UID',
            width: 100,
            align: "center",
            valign: "middle",
            field: 'uid',
        }, {
            title: '昵称',
            width: 150,
            align: "center",
            valign: "middle",
            field: 'nickname',
        }, {
            title: '信息',
            width: 200,
            align: "center",
            valign: "middle",
            field: 'message'
        },
        {
            field: 'createTime',
            align: "center",
            valign: "middle",
            width: 150,
            title: '创建时间',
            formatter: function (value) {
                if (value) {
                    value = parseInt(value);
                    return new Date(value).toLocaleString();
                }
            }
        },
        {
            field: 'reply',
            align: "center",
            valign: "middle",
            width: 150,
            title: '回复内容'
        },
        {
            field: 'replyTime',
            align: "center",
            valign: "middle",
            width: 150,
            title: '回复时间',
            formatter: function (value) {
                if (value) {
                    value = parseInt(value);
                    return new Date(value).toLocaleString();
                }
            }
        },
        {
            title: '操作',
            width: 200,
            align: "center",
            valign: "middle",
            formatter: function (value, row, index) {

                let htm = ``;
                htm += `<button type = "button" class="btn btn-light"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="回复" onClick = 'showAddGold("${row._id}", ${row.uid})' ><i class="bi bi-envelope fs-5 text-success"></i></button > `;
                return htm;
            }
        }]
    });
    let permission = localStorage.getItem('permission', '');
    if (permission != -1) {
        $("#testDiv").remove();
        $("#masterDiv").remove();
    } else {
        $("#adminDiv").remove();
    }
    createList();
});

function avaterImg(value, row, index) {
    var htm = "";
    htm = `<img  style="width: 40px ; height: 40px;"  src="${value}" class="img-thumbnail" >`;
    return htm;
}